package com.hqyj.paimaisys.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component
public class MailUtil {

    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    JavaMailSender javaMailSender;

    public void sendMimeMail() throws MessagingException {
        MimeMessage mimeMailMessage =  javaMailSender.createMimeMessage();

        //第二个参数，代表是否可以发送附件
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage,true);

        mimeMessageHelper.setFrom(from);

        mimeMessageHelper.setTo(from);
        mimeMailMessage.setSubject("邮件标题");
        mimeMessageHelper.setText("<a href='#'>测试右键</a>", true);

        javaMailSender.send(mimeMailMessage);
    }

    // update huiyuan set rid = 1 where hid = ?
    //发激活邮件
    public void sendJihuoMail(String to, String hid) throws MessagingException {
        MimeMessage mimeMailMessage =  javaMailSender.createMimeMessage();

        //第二个参数，代表是否可以发送附件
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage,true);
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);
        mimeMailMessage.setSubject("拍卖网站的激活邮件");
        mimeMessageHelper.setText("恭喜你:<br> 您已经成为会员，<a href='http://localhost:8080/huiyuan/active?hid="
                + hid + "'>请点击该链接激活</a>", true);

        javaMailSender.send(mimeMailMessage);
    }
}
